$(".inp").focusin(function() {
	inp_tips($(this));
}).focusout(function() {
	chech_form($(this));
});

$("#submit").click(function(){
	$.post("?ajax=check", $("form:first").serialize(), function(data){
		if(data.err == 1) {
			inp_tips($(".inp[name='"+data.name+"']"));
			$(".inp[name='"+data.name+"']").focus().after("<div class='tips_no'>"+data.msg+"</div>");
		}else if(data.err == 2) {
			var r = confirm(data.msg);
			if(r) location.href = $("#submit").attr("url");
		}else {
			location.href = $("#submit").attr("url");
		}
	}, "json");
});

function inp_tips(obj) {
	obj.parent().children("div").remove();
	obj.before("<div class='inp_tips'>"+obj.attr("tips")+"</div>");
	$(".inp_tips").css({top: obj.position().top-25, left: obj.position().left+50});
}

function chech_form(obj) {
	obj.parent().children("div").remove();
	var err, load, val=obj.val();
	switch(obj.attr("name")) {
		case "dbhost":
			if(!val) err = "主机不能为空";
			break;
		case "dbuser":
			if(!val) err = "数据库用户名不能为空";
			break;
		case "dbpw":
			load = true;
			$.post("?ajax=check_db", $("form:first").serialize(), function(data){
				obj.parent().children("div").remove();
				if(data.msg == "error_connect") {
					$(".inp[name='dbpw']").after("<div class='tips_no'>连接MySql失败，数据库用户名和密码是否正确？</div>");
				}else{
					$(".inp[name='dbpw']").after("<div class='tips_yes'></div>");
				}
			}, "json");
			break;
		case "dbname":
			if(!val) err = "数据库名不能为空";
			else{
				load = true;
				$.post("?ajax=check_dbname", $("form:first").serialize(), function(data){
					obj.parent().children("div").remove();
					if(data.msg == "error_connect") {
						$(".inp[name='dbname']").after("<div class='tips_no'>连接MySql失败，数据库用户名和密码是否正确？</div>");
					}else if(data.msg == "error_select") {
						$(".inp[name='dbname']").after("<div class='tips_look'>数据库不存在，下一步时将自动创建</div>");
					}else{
						$(".inp[name='dbname']").after("<div class='tips_yes'></div>");
					}
				}, "json");
			}
			break;
		case "dbpre":
			if(!val) err = "数据库表前辍不能为空";
			else if(!/^\w+$/.test(val)) err = "数据库表前辍错误";
			else{
				load = true;
				$.post("?ajax=check_dbpre", $("form:first").serialize(), function(data){
					obj.parent().children("div").remove();
					if(data.msg == "error_connect") {
						$(".inp[name='dbpre']").after("<div class='tips_no'>连接MySql失败，数据库用户名和密码是否正确？</div>");
					}else if(data.msg == "exist_dbpre") {
						$(".inp[name='dbpre']").after("<div class='tips_look'>表前辍已经存在, 请修改表前辍！</div>");
					}else{
						$(".inp[name='dbpre']").after("<div class='tips_yes'></div>");
					}
				}, "json");
			}
			break;
		case "auser":
			if(!val) err = "用户名不能为空";
			break;
		case "apswd":
			if(!val) err = "密码不能为空";
			else if(/ /.test(val)) err = "密码不能有空格";
			else if(val.length<6) err = "密码不能小于6位数";
			break;
		case "founder_name":
			if(!val) err = "姓名不能为空";
			else if(/[^\u4e00-\u9fa5]/.test(val)) err = "姓名只能是汉字";
			else if(!/^[\u4e00-\u9fa5]{2,}$/.test(val)) err = "姓名不能少于两个汉字";
			break;
		case "founder_phone":
			if(!val) err = "手机号不能为空";
			else if(/[^\d]+/.test(val)) err = "不能有非数字";
			else if(!/^1[358]/.test(val)) err = "手机号前两位错误";
			else if(val.length<11) err = "手机号不够11位";
			else if(val.length>11) err = "手机号已超过11位";
			break;
		case "founder_mail":
			if(!val) err = "邮箱不能为空";
			else if(!/^[\w\-\.]+@[\w\-][\w\-\.]+\.[a-z]{2,}$/.test(val)) err = "邮箱不正确";
			break;
	}
	if(load) {
		obj.after("<div class='tips_load'></div>");
	}else if(err) {
		obj.after("<div class='tips_no'>"+err+"</div>");
	}else{
		obj.after("<div class='tips_yes'></div>");
	}
}